package cn.com.jslh.boarpro.util.template;

import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;

import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.main.view.SelectDialog;
import cn.com.jslh.boarpro.main.view.fragment.AddTemplateFragment;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * Created by Administrator on 2017/11/17.
 */

public class SelectSearchTempalte extends AddTempBaseItem implements AdapterInterface {
    @Override
    public String getFlag() {
        return "selectSearch";
    }

    @Override
    public int getLayout() {
        return R.layout.add_template_selectsearch_item_layout;
    }

    @Override
    public SlimInjector<? extends AddTempBaseItem> getSlimInjetor() {
        SlimInjector<SelectSearchTempalte> inj = new SlimInjector<SelectSearchTempalte>() {
            @Override
            public void onInject(final SelectSearchTempalte data, IViewInjector injector) {
                JSONObject config = new JSONObject();
                JSONArray matterarry = null;
                try {
                    config = new JSONObject(data.getConfig());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int isCheck = 0; // 0 不需检测 1检测
                String checkAction = "";
                String searchAction = "";
                try {
                    checkAction = config.getString("checkAction");
                    searchAction = config.getString("searchAction");
                    isCheck = config.getInt("isCheck");
                    matterarry = config.getJSONArray("matterarry");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                final JSONObject finalConfig = config;
                final String finalSearchAction = searchAction;
                final JSONArray finalMatterarry = matterarry;
                injector.text(R.id.titleText, data.getTitle())
                        .with(R.id.valueText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                TextView textView = (TextView) view;
                                String placeholder = data.getPlaceholder();
                                textView.setText(placeholder);
                                JSONUtil.configValueTextView(textView, finalConfig);
                            }
                        })
                        .with(R.id.titleText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                JSONUtil.configTextView((TextView) view, finalConfig);
                            }
                        }).clicked(R.id.valueText, new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        final SelectDialog dialog = new SelectDialog.Builder(v.getContext()).create();
                        dialog.setDatas(JSONUtil.JsonArray2List(finalMatterarry));
                        dialog.setBase(finalSearchAction);
                        dialog.setItemClickListener(new SelectDialog.SelectChoseListener() {
                            @Override
                            public void selectItem(String id, String title) {
                                data.setResult(id);
                                data.setPlaceholder(title);
                                TemplateHelper.updataItems(v.getContext(), data);
                                AddTemplateFragment.notifyDatas();
                            }
                        });
                        dialog.show();
                    }
                });
            }
        };
        return inj;
    }
}
